//
//  TopRankCell.swift
//  CoolTennisBall
//
//  Created by CA on 16/6/18.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

import UIKit

class TopRankCell: UITableViewCell {

    @IBOutlet weak var noOneHeadIcon: DesignableImageView!
    @IBOutlet weak var noOneNameLabel: UILabel!
    @IBOutlet weak var noOneValueLabel: UILabel!
    @IBOutlet weak var noOneUnitLabel: UILabel!
    
    @IBOutlet weak var noTwoHeadIcon: DesignableImageView!
    @IBOutlet weak var noTwoNameLabel: UILabel!
    @IBOutlet weak var noTwoValueLabel: UILabel!
    @IBOutlet weak var noTwoUnitLabel: UILabel!
    
    @IBOutlet weak var noThreeHeadIcon: DesignableImageView!
    @IBOutlet weak var noThreeNameLabel: UILabel!
    @IBOutlet weak var noThreeValueLabel: UILabel!
    @IBOutlet weak var noThreeUnitLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        addGestureRecognizers()
    }
    
    func configureWithTopRankItems(_ items: [RankModel], rankModeType: RankModeType) {
        
        switch rankModeType {
        case .maxSpeed:
            noOneUnitLabel.text = NSLocalizedString("km/h", comment: "")
            noTwoUnitLabel.text = NSLocalizedString("km/h", comment: "")
            noThreeUnitLabel.text = NSLocalizedString("km/h", comment: "")
        case .swings:
            noOneUnitLabel.text = NSLocalizedString("Sport.Unit.Shots", comment: "")
            noTwoUnitLabel.text = NSLocalizedString("Sport.Unit.Shots", comment: "")
            noThreeUnitLabel.text = NSLocalizedString("Sport.Unit.Shots", comment: "")
        case .avgScore:
            noOneUnitLabel.text = NSLocalizedString("Unit.Score", comment: "")
            noTwoUnitLabel.text = NSLocalizedString("Unit.Score", comment: "")
            noThreeUnitLabel.text = NSLocalizedString("Unit.Score", comment: "")
        }
        
        for (index, item) in items.enumerated() {
            switch index {
            case 0:
                noOneHeadIcon.sd_setImage(with: URL(string: item.headIcon), placeholderImage: UIImage(named: "competition_icon_defaulImage"))
                noOneNameLabel.text = item.userName
                noOneValueLabel.text = item.value(with: rankModeType)
            case 1:
                noTwoHeadIcon.sd_setImage(with: URL(string: item.headIcon), placeholderImage: UIImage(named: "competition_icon_defaulImage"))
                noTwoNameLabel.text = item.userName
                noTwoValueLabel.text = item.value(with: rankModeType)
                
            case 2:
                noThreeHeadIcon.sd_setImage(with: URL(string: item.headIcon), placeholderImage: UIImage(named: "competition_icon_defaulImage"))
                noThreeNameLabel.text = item.userName
                
                noThreeValueLabel.text = item.value(with: rankModeType)
            default: break
            }
        }
    }
    
    fileprivate func addGestureRecognizers() {
        let tap1 = UITapGestureRecognizer(target: self, action: #selector(TopRankCell.showHeadIcon(_:)))
        let tap2 = UITapGestureRecognizer(target: self, action: #selector(TopRankCell.showHeadIcon(_:)))
        let tap3 = UITapGestureRecognizer(target: self, action: #selector(TopRankCell.showHeadIcon(_:)))
        noOneHeadIcon.addGestureRecognizer(tap1)
        noTwoHeadIcon.addGestureRecognizer(tap2)
        noThreeHeadIcon.addGestureRecognizer(tap3)
        
    }
    
    @objc fileprivate func showHeadIcon(_ gestureRecognizer: UIGestureRecognizer) {
        guard let imageView = gestureRecognizer.view as? UIImageView else { return }
        imageView.showImage()
    }
    
}
